package com.glodon.paas.activity.dao.mongo;

import java.util.List;

import org.apache.commons.lang.StringUtils;

import com.glodon.paas.activity.dao.AppInfoDao;
import com.glodon.paas.activity.dao.domain.AppInfoEntity;
import com.glodon.paas.util.DateUtil;
import com.google.code.morphia.query.Query;
import com.google.code.morphia.query.UpdateOperations;

public class AppInfoDaoMongoImpl extends MongoDBDao<AppInfoEntity, Integer> implements AppInfoDao {

    @Override
    public Integer insertSelective(AppInfoEntity record) {
        return insert(record);
    }

    @Override
    public int updateByPrimaryKeySelective(AppInfoEntity record) {
        if (record == null || record.getAppId() == null) {
            return 0;
            // throw PaasServerExceptionFactory.validateException("id is null", null);
        }
        Query<AppInfoEntity> query = createQuery().filter("appId", record.getAppId());
        UpdateOperations<AppInfoEntity> update = this.createUpdateOperations().set("updateTime", DateUtil.getNow());
        if (StringUtils.isNotBlank(record.getDisplayName())) {
            update.set("displayName", record.getDisplayName());
        }

        if (StringUtils.isNotBlank(record.getDescription())) {
            update.set("description", record.getDescription());
        }

        return super.updateFirst(query, update);
    }

    @Override
    public List<AppInfoEntity> selectAll() {
        Query<AppInfoEntity> q = this.createQuery();
        q.order("_id");
        return q.asList();
    }

    @Override
    public int deleteByPrimaryKey(Integer id) {
        return deleteById(id);
    }

    @Override
    public AppInfoEntity selectByPrimaryKey(Integer id) {
        return selectById(id);
    }
}
